package com.danawa.watcher.validation;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;

import com.danawa.watcher.model.Registration;

@Component("registrationValidator")
public class RegistrationValidator {

	public boolean supports(Class<?> klass) {
		return Registration.class.isAssignableFrom(klass);
	}

	public void validate(Object target, Errors errors) {
		Registration registration = (Registration) target;

		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email",
				"NotEmpty.registration.email", "이메일을 입력해 주세요");
		
		
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password",
				"NotEmpty.registration.email", "비밀번호를 입력해 주세요");
		
		String password = registration.getPassword();
		if ((password.length()) < 6) {
			errors.rejectValue("password", "lengthOfUser.registration.password", "패스워드 길이는 6자 이상 입력해주세요");
		}
		
		if (!(registration.getPassword()).equals(registration.getConfirmPassword())) {
			errors.rejectValue("password",
					"matchingPassword.registration.password",
					"비밀번호가 일치하지 않습니다");
		}
		
		

	}
}
